//
//  YWWriteMeterReadingNumberCell.m
//  maintain
//
//  Created by Kyo on 16/8/20.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
#import "YWWriteMeterReadingNumberCell.h"

@interface YWWriteMeterReadingNumberCell () <UITextFieldDelegate>
@property (nonatomic, strong) UILabel* thisReadingLabel;
@property (nonatomic, strong) UITextField* thisTextField;
@end

@implementation YWWriteMeterReadingNumberCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpSubviews];
    }

    return self;
}

- (void)setPlaceholderStr:(NSString *)placeholderStr{
    _placeholderStr = placeholderStr;
    _thisTextField.placeholder = _placeholderStr;
}

- (UILabel*)thisReadingLabel
{
    if (!_thisReadingLabel) {
        UILabel* thisReadingLabel = [[UILabel alloc] init];
        thisReadingLabel.font = kContentFont;
        thisReadingLabel.text = @"本次读数:";
        //        thisReadingLabel.textColor = [UIColor customTextGrayColor];
        [self.contentView addSubview:thisReadingLabel];
        _thisReadingLabel = thisReadingLabel;
    }
    return _thisReadingLabel;
}

- (UITextField*)thisTextField
{
    if (!_thisTextField) {
        UITextField* thisTextField = [[UITextField alloc] init];
        thisTextField.font = kContentFont;
        thisTextField.placeholder = @"填写读数(使用量 = 读数*倍率)";
        thisTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        thisTextField.layer.borderWidth = 1.0;
        thisTextField.clipsToBounds = YES;
        thisTextField.layer.cornerRadius = 4.0;
        thisTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 1)];
        thisTextField.leftViewMode = UITextFieldViewModeAlways;
        thisTextField.keyboardType = UIKeyboardTypeDecimalPad;
        thisTextField.delegate = self;
        [self.contentView addSubview:thisTextField];
        _thisTextField = thisTextField;
    }
    return _thisTextField;
}

- (void)setUpSubviews
{
    [self.thisReadingLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentView).offset(5);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.width.mas_equalTo(@70);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
        make.height.mas_equalTo(34);
    }];

    [self.thisTextField mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentView).offset(5);
        make.left.mas_equalTo(_thisReadingLabel.mas_right).offset(0);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.bottom.mas_equalTo(_thisReadingLabel.mas_bottom);
    }];
}
- (void)textFieldDidEndEditing:(UITextField*)textField
{
    NSString* value = [self.thisTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([self.delegate respondsToSelector:@selector(transmitTextFieldValue:cell:)]) {
        [self.delegate transmitTextFieldValue:value cell:self];
    }
}
#define myDotNumbers @"0123456789.\n"
#define myNumbers @"0123456789\n"
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string

{
    NSCharacterSet* cs;
    NSUInteger nDotLoc = [textField.text rangeOfString:@"."].location;
    if (NSNotFound == nDotLoc && 0 != range.location) {
        cs = [[NSCharacterSet characterSetWithCharactersInString:myDotNumbers] invertedSet];
    }
    else {
        cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];
    }
    NSString* filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    BOOL basicTest = [string isEqualToString:filtered];
    if (!basicTest) {
        [YWCommonTool showTextHUDWithContent:@"输入内容无效"];
        return NO;
    }
    if (NSNotFound != nDotLoc && range.location > nDotLoc + 2) {
        [YWCommonTool showTextHUDWithContent:@"小数点后最多2位"];
        return NO;
    }
    return YES;
}
@end
